<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-model-item label="日期">
              <a-range-picker
                style="width: 100%"
                :placeholder="['日期', '日期']"
                v-model="updateTime"
                date-format="YYYY-MM-DD"
                @change="onDateChange"
              />
            </a-form-model-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        :columns="columns"
        @expandedRowRender="expandedRowRender"
        :data-source="dataSource"
        :rowKey="(item) => item.id"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="operation" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetails(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a @click="set(record)">{{ record.isBroadcast === 1 ? '取消播报' : '设为播报' }}</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <a-table
          slot="expandedRowRender"
          slot-scope="record"
          :columns="innerColumns"
          :data-source="record.value"
          :pagination="false"
        >
          <span slot="proportion" slot-scope="text"> {{ getNum(text) }} </span>
        </a-table>
      </a-table>
    </div>
    <everyDayPriceMsg-modal ref="modalForm" @ok="modalFormOk" @close="close"></everyDayPriceMsg-modal>
  </a-card>
</template>

<script>
import { getAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import EveryDayPriceMsgModal from './modal/everyDayPriceMsgModal.vue'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import YearSelect from '@/components/jeecg/YearDateSelect.vue'
import { dpdeletetqppOfferPrice, dpevchangeBroadOn, dpevchangeBroadOff } from '@/api/dashboardPaltform'
import NP from 'number-precision'
export default {
  name: 'varietyList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    YearSelect,
    JDictSelectTag,
    EveryDayPriceMsgModal,
  },
  data() {
    return {
      updateTime: [],
      createTime: [],
      positionList: [],
      description: '每日价格播报',
      innerColumns: [
        { title: '品种', dataIndex: 'varietiesName', key: 'varietiesName' },
        { title: '单品种均价(元/斤)', dataIndex: 'price', key: 'price' },
      ],
      // 表头
      columns: [
        { title: '日期', dataIndex: 'priceDate', key: 'priceDate' },
        { title: '平均价(元/斤)', dataIndex: 'averagePrice', key: 'averagePrice' },
        { title: '最低价(元/斤)', dataIndex: 'lowestPrice', key: 'lowestPrice' },
        { title: '最高价(元/斤)', dataIndex: 'highestPrice', key: 'highestPrice' },
        { title: '操作', key: 'operation', scopedSlots: { customRender: 'operation' } },
      ],
      dataSource: [],
      innerData: [],
      isorter: {},
      url: {
        list: '/system/tqppOfferPrice/list',
        delete: '/system/tqppOfferPrice/delete',
      },
      dictOptions: {},
    }
  },
  mounted() {
    this.getTxppSubjectList()
  },
  computed: {},
  methods: {
    searchReset() {
      this.updateTime = undefined
      this.queryParam.priceDate_begin = undefined
      this.queryParam.priceDate_end = undefined
      this.searchQuery()
    },
    close() {
      this.searchQuery()
    },
    getNum(val) {
      return NP.round(val, 2) + '%'
    },
    clickdelete(id) {
      this.$confirm({
        title: '您确定要删除该条记录吗？',
        onOk: () => {
          this.dpdeletetqppOfferPrice(id)
        },
        onCancel() {
          console.log('Cancel')
        },
        centered: true,
      })
    },
    set(data) {
      console.log(data.isBroadcast)
      if (data.isBroadcast === 1) {
        this.$confirm({
          title: '是否确定取消该条数据的播报',
          onOk: () => {
            this.dpevchangeBroadOff(data.id)
          },
          onCancel() {
            console.log('Cancel')
          },
          centered: true,
        })
      } else {
        this.$confirm({
          title: '确定将该条数据设为播报？该操作将会取消当前播报的数据,是否继续？',
          onOk: () => {
            this.dpevchangeBroadOn(data.id)
          },
          onCancel() {
            console.log('Cancel')
          },
          centered: true,
        })
      }
    },
    async dpdeletetqppOfferPrice(val) {
      let res = await dpdeletetqppOfferPrice({ id: val })
      if (res.success) {
        this.$message.success(res.message)
        this.searchQuery()
      } else {
        this.$message.error(res.message)
      }
    },
    async dpevchangeBroadOn(val) {
      let res = await dpevchangeBroadOn({ id: val })
      if (res.success) {
        this.$message.success(res.result)
        this.searchQuery()
      } else {
        this.$message.error(res.message)
      }
    },
    async dpevchangeBroadOff(val) {
      let res = await dpevchangeBroadOff({ id: val })
      if (res.success) {
        this.$message.success(res.result)
        this.searchQuery()
      } else {
        this.$message.error(res.message)
      }
    },
    getTxppSubjectList() {
      getAction('/loquat/txppSubject/allList').then((res) => {
        this.positionList = res.result
      })
    },
    initDictConfig() {},
    // 选择时间
    onDateChange(date, dateString) {
      this.updateTime = date
      if (dateString && dateString.length) {
        this.queryParam.priceDate_begin = dateString[0]
        this.queryParam.priceDate_end = dateString[1]
      } else {
        this.queryParam.priceDate_begin = undefined
        this.queryParam.priceDate_end = undefined
      }
    },
  },
}
</script>
<style scoped lang="less">
@import '~@assets/less/common.less';
.table-page-search-wrapper {
  /deep/ .ant-calendar-range-picker-input {
    font-size: 12px;
  }
}
</style>